/**
 * $Id: PersonnelServiceProxy.java 2 2011-09-03 10:16:43Z adriftrock@gmail.com $
 */

package com.swehr.proxy;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.swehr.model.Organization;
import com.swehr.model.Personnel;
import com.swehr.model.Position;
import com.swehr.model.ResultInfo;
import org.apache.log4j.Logger;

import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * @author Rock Huang
 * @version 0.1
 */
public class PersonnelServiceProxy {
    private final static Logger logger = Logger.getLogger(PersonnelServiceProxy.class);

    // the URL of RESTful web services
//    private final static String URL = "http://174.129.108.178/organizations";
    private final static String URL = "http://localhost:8080/organization";
    private final static String PSN_URL = "http://localhost:8080/personnel";
    private final static String POS_URL = "http://localhost:8080/position";

    public Organization add(Organization org) {
        WebResource resource = getWebResource();
        try {
            org = resource.type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(Organization.class, org);
        } catch (Exception e) {
            logger.error(e);
            org.setComment(e.getMessage());
        }
        logger.debug("Actual acquisition: "  + org);

        return org;
    }

    public Organization update(Organization org) {
        WebResource resource = getWebResource();
        Organization o = org;
        try {
            o = resource.type(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON)
                    .put(Organization.class, org);
        } catch (UniformInterfaceException ue) {
            logger.error(ue);
        }
        logger.debug("Actual acquisition: " + o);

        return o;
    }

    public ResultInfo remove(int id) {
        WebResource resource = getWebResource(URL + "/" + id);
        ResultInfo info = new ResultInfo();
        try {
            info = resource.delete(ResultInfo.class, info);
        } catch (Exception e) {
            logger.error(e);
            info.setMessage(e.getMessage());
            info.setSuccess(false);
        }
        return info;
    }

    public Organization signin(Organization org) {
        WebResource resource = getWebResource(URL + "/signin");
        try {
            org = resource.type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(Organization.class, org);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            org.setComment(e.getMessage());
        }
        logger.debug("Actual acquisition: "  + org);
        return org;
    }

    public ResultInfo save(List<Organization> orgs) {
        WebResource resource = getWebResource(URL + "/save");
        ResultInfo info = new ResultInfo();
        try {
            info = resource.type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(ResultInfo.class, new GenericEntity<List<Organization>>(orgs){});
        } catch (Exception e) {
            logger.error(e);
            info.setMessage("Exception: " + e.getMessage());
            info.setSuccess(false);
        }
        return info;
    }

    public ResultInfo savePsn(List<Personnel> psns) {
        WebResource resource = getWebResource(PSN_URL + "/save");
        ResultInfo info = new ResultInfo();
        try {
            info = resource.type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(ResultInfo.class, new GenericEntity<List<Personnel>>(psns){});
        } catch (Exception e) {
            logger.error(e);
            info.setMessage("Exception: " + e.getMessage());
            info.setSuccess(false);
        }
        return info;
    }

    public ResultInfo savePos(List<Position> pos) {
        WebResource resource = getWebResource(POS_URL + "/save");
        ResultInfo info = new ResultInfo();
        try {
            info = resource.type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(ResultInfo.class, new GenericEntity<List<Position>>(pos){});
        } catch (Exception e) {
            logger.error(e);
            info.setMessage("Exception: " + e.getMessage());
            info.setSuccess(false);
        }
        return info;
    }

    private WebResource getWebResource(String u) {
        Client client = Client.create();
        client.addFilter(new LoggingFilter());

        // the setting is for google app engine, which wasted me lots of time :(
        client.setReadTimeout(100000);

        return client.resource(u == null ? URL : u);
    }

    private WebResource getWebResource() {
        return getWebResource(null);
    }
}



